package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.PersonnelChange;
import com.amsp.oa.oademo.entity.PersonnelChangeDetail;
import com.amsp.oa.oademo.mapper.PersonnelChangeMapper;
import com.amsp.oa.oademo.mapper.PersonnelChangeDetailMapper;
import com.amsp.oa.oademo.service.PersonnelChangeService;
import com.amsp.oa.oademo.vo.PersonnelChangeDetailVO;
import com.amsp.oa.oademo.vo.PersonnelChangeVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PersonnelChangeServiceImpl implements PersonnelChangeService {

    @Resource
    private PersonnelChangeMapper personnelChangeMapper;

    @Resource
    private PersonnelChangeDetailMapper personnelChangeDetailMapper;

    @Override
    public PersonnelChangeVO getById(Long id) {
        PersonnelChange personnelChange = personnelChangeMapper.selectById(id);
        if (personnelChange == null) {
            return null;
        }

        PersonnelChangeVO vo = new PersonnelChangeVO();
        BeanUtils.copyProperties(personnelChange, vo);

        List<PersonnelChangeDetail> details = personnelChangeDetailMapper.selectList(
                new LambdaQueryWrapper<PersonnelChangeDetail>()
                        .eq(PersonnelChangeDetail::getChangeId, id));

        vo.setPersonnelChangeList(details.stream().map(detail -> {
            PersonnelChangeDetailVO detailVO = new PersonnelChangeDetailVO();
            BeanUtils.copyProperties(detail, detailVO);
            if (detail.getEffectiveDate() != null) {
                detailVO.setEffectiveDate(detail.getEffectiveDate().toString());
            }
            return detailVO;
        }).collect(Collectors.toList()));

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(PersonnelChangeVO personnelChangeVO) {
        PersonnelChange personnelChange = new PersonnelChange();
        BeanUtils.copyProperties(personnelChangeVO, personnelChange);

        if (personnelChange.getId() != null) {
            personnelChangeMapper.updateById(personnelChange);
            // 删除原有明细
            personnelChangeDetailMapper.delete(
                    new LambdaQueryWrapper<PersonnelChangeDetail>()
                            .eq(PersonnelChangeDetail::getChangeId, personnelChange.getId()));
        } else {
            personnelChangeMapper.insert(personnelChange);
        }

        // 保存新的明细
        if (personnelChangeVO.getPersonnelChangeList() != null) {
            List<PersonnelChangeDetail> details = personnelChangeVO.getPersonnelChangeList().stream()
                    .map(detailVO -> {
                        PersonnelChangeDetail detail = new PersonnelChangeDetail();
                        BeanUtils.copyProperties(detailVO, detail);
                        detail.setChangeId(personnelChange.getId());
                        if (detailVO.getEffectiveDate() != null) {
                            detail.setEffectiveDate(LocalDate.parse(detailVO.getEffectiveDate()));
                        }
                        return detail;
                    }).collect(Collectors.toList());

            details.forEach(personnelChangeDetailMapper::insert);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long id) {
        personnelChangeDetailMapper.delete(
                new LambdaQueryWrapper<PersonnelChangeDetail>()
                        .eq(PersonnelChangeDetail::getChangeId, id));
        personnelChangeMapper.deleteById(id);
    }

    @Override
    public IPage<PersonnelChangeVO> page(Integer current, Integer size, String monthValue, String docNumber,
            String reviewer, String approver) {
        Page<PersonnelChange> page = personnelChangeMapper.selectPage(
                new Page<>(current, size),
                new LambdaQueryWrapper<PersonnelChange>()
                        .like(StringUtils.hasText(monthValue), PersonnelChange::getMonthValue, monthValue)
                        .like(StringUtils.hasText(docNumber), PersonnelChange::getDocNumber, docNumber)
                        .like(StringUtils.hasText(reviewer), PersonnelChange::getReviewer, reviewer)
                        .like(StringUtils.hasText(approver), PersonnelChange::getApprover, approver)
                        .orderByDesc(PersonnelChange::getCreateTime));

        IPage<PersonnelChangeVO> voPage = new Page<>(current, size, page.getTotal());
        List<PersonnelChangeVO> records = page.getRecords().stream().map(record -> {
            PersonnelChangeVO vo = new PersonnelChangeVO();
            BeanUtils.copyProperties(record, vo);
            return vo;
        }).collect(Collectors.toList());
        voPage.setRecords(records);

        return voPage;
    }
}